/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Bryan
 * Adds a new agent into the database
 */
public class AddAgentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
         Connection con = DBConnector.connect();
 
            try{
            String agentcode = request.getParameter("agentcode");
            String lastname = request.getParameter("lastname");
            String firstname = request.getParameter("firstname");
            String middlename = request.getParameter("middlename");
            String contact = request.getParameter("contact");
            String email = request.getParameter("email");
            String street = request.getParameter("street");
            String city = request.getParameter("city");
            String zip = request.getParameter("zip");
            String remarks = request.getParameter("remarks");
            String commission = request.getParameter("commission");
            String name = firstname+' '+middlename+' '+lastname;

                    PreparedStatement ps = con.prepareStatement
                            ("insert into quicklenddb.agent(agentcode,a_name,a_contact,a_email,"
                            + "a_street,a_city,a_zip,a_remarks,commission)"
                            + "values(?,?,?,?,?,?,?,?,?)");

                    ps.setString(1, agentcode);
                    ps.setString(2, name);
                    ps.setString(3, contact);
                    ps.setString(4, email);
                    ps.setString(5, street);
                    ps.setString(6, city);
                    ps.setString(7, zip);
                    ps.setString(8, remarks);
                    ps.setString(9, commission);
                    ps.executeUpdate();


                response.sendRedirect("AgentServlet");
         
            }catch(SQLException e)
            {
                System.out.println(e); 
                response.sendRedirect("fail.jsp");
                //Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, e);
            }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
